﻿CREATE PROCEDURE [dbo].[yaf_user_login](
                @BoardID  INT,
                @Name     NVARCHAR(50),
                @Password NVARCHAR(32))
AS
    BEGIN
        DECLARE  @UserID INT
        -- Try correct board first
        IF EXISTS (SELECT UserID
                   FROM   yaf_User
                   WHERE  Name = @Name
                   AND Password = @Password
                   AND BoardID = @BoardID
                   AND (Flags & 2) = 2)
        BEGIN
            SELECT UserID
            FROM   yaf_User
            WHERE  Name = @Name
            AND Password = @Password
            AND BoardID = @BoardID
            AND (Flags & 2) = 2
            RETURN
        END
        IF NOT EXISTS (SELECT UserID
                       FROM   yaf_User
                       WHERE  Name = @Name
                       AND Password = @Password
                       AND (BoardID = @BoardID
                             OR (Flags & 3) = 3))
        SET @UserID = NULL
        ELSE
        SELECT @UserID = UserID
        FROM   yaf_User
        WHERE  Name = @Name
        AND Password = @Password
        AND (BoardID = @BoardID
              OR (Flags & 1) = 1)
        AND (Flags & 2) = 2
        SELECT @UserID
    END


